package com.zwq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zwq.domain.PmsBrand;
import com.zwq.domain.PmsCategory;
import com.zwq.domain.PmsSpuInfo;
import com.zwq.repository.BusinessReposiness;
import com.zwq.service.PmsBrandService;
import com.zwq.service.PmsCategoryService;
import com.zwq.service.PmsSpuInfoService;
import com.zwq.util.R;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
@CrossOrigin
public class OrderController {
    @Resource
    PmsSpuInfoService pmsSpuInfoService;
    @Resource
    PmsCategoryService pmsCategoryService;
    @Autowired
    BusinessReposiness businessReposiness;
    @Resource
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    PmsBrandService pmsBrandService;
    @Resource
    PmsCategoryService pmsCategoryService1;

    @RequestMapping("/list")
    public R list(@RequestBody PmsSpuInfo pmsSpuInfo) {
        PageHelper.startPage(pmsSpuInfo.getNum(), pmsSpuInfo.getSize());
        List<PmsSpuInfo> list = pmsSpuInfoService.lists(pmsSpuInfo);
        PageInfo<PmsSpuInfo> infoPageInfo = new PageInfo<>(list);
        return R.OK(infoPageInfo);
    }

//    @Cacheable({"productionText"})
//    public List<PmsSpuInfo> findAttrList() {
//                String pmsSpuInfoText = stringRedisTemplate.opsForValue().get("pmsSpuInfoText");
//        if(pmsSpuInfoText!=null&&!"".equals(pmsSpuInfoText)){
//            List<PmsSpuInfo> list = JSONArray.parseArray(pmsSpuInfoText,PmsSpuInfo.class);
//            return list;
//        }else{
//            List<PmsSpuInfo> lists=pmsSpuInfoService.list();
//            for (int i=0 ;i<lists.size();i++){
//                PmsSpuInfo pmsSpuInfo = lists.get(i);
//                Long attrId = pmsSpuInfo.getId();
//                List<PmsSkuInfo> attrName=productionMapper.findAttrName(attrId);
//                pmsAttr.setAttrNameList(attrName);
//            }
//            String attrLists = JSON.toJSONString(lists);
//            stringRedisTemplate.opsForValue().set("attrListText",attrLists,2, TimeUnit.DAYS);
//            System.out.println("属性值:"+lists.toString());
//            return lists;
//            }
//        }
//    }
    @RequestMapping("/add")
    public R add(@RequestBody PmsSpuInfo pmsSpuInfo){
        if (pmsSpuInfo.getId()==null){
            pmsSpuInfo.setCreateTime(new Date());
        }else {
            pmsSpuInfo.setUpdateTime(new Date());
        }
        boolean save = pmsSpuInfoService.saveOrUpdate(pmsSpuInfo);
        return R.OK(save);
    }
    @RequestMapping("/del")
    public R del(int id){
        boolean b = pmsSpuInfoService.removeById(id);
        return R.OK(b);
    }
    @RequestMapping("/category")
    public R category(){
        List<PmsCategory> list = pmsCategoryService.list();
        return R.OK(list);
    }

//    同步到Es
//    @PostConstruct
//    public void test1(){
//        List<PmsSpuInfo> list = pmsSpuInfoService.list();
//        businessReposiness.saveAll(list);
//    }

    @RequestMapping("/esList")
    public R esList() {

//        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//
//        if (StringUtils.isNotBlank(pmsSpuInfo.getSpuName())) {
//            boolQuery.must(QueryBuilders.matchQuery("spuName", pmsSpuInfo.getSpuName()));
//        }

        NativeSearchQuery query = new NativeSearchQueryBuilder().build();
//                .withQuery(boolQuery).build();

        SearchHits<PmsSpuInfo> search = elasticsearchRestTemplate.search(query, PmsSpuInfo.class);
        List<SearchHit<PmsSpuInfo>> searchHits = search.getSearchHits();

        ArrayList<PmsSpuInfo> list = new ArrayList<>();

        for (SearchHit<PmsSpuInfo> searchHit : searchHits) {
            PmsSpuInfo content = searchHit.getContent();
            list.add(content);
        }

        Map map = new HashMap();
        Map map1 = new HashMap<>();
        map.put("list", list);
        return R.OK(map);
    }
    @GetMapping("/cheshi")
    public R cheshi() {
        return R.OK(ceShi());
    }
    private ThreadPoolExecutor executor = new  ThreadPoolExecutor(20,200,100L,
            TimeUnit.SECONDS,new LinkedBlockingQueue<>(),new ThreadPoolExecutor.CallerRunsPolicy());


    public Map ceShi(){
        Map map = new HashMap<>();
        Future<List<PmsSpuInfo>> future = executor.submit(() -> pmsSpuInfoService.list());
        Future<List<PmsCategory>> future1 = executor.submit(() -> pmsCategoryService1.list());
        Future<List<PmsBrand>> future2 = executor.submit(() -> pmsBrandService.list());

        try {
            List<PmsSpuInfo> list = future.get();
            List<PmsCategory> list1 = future1.get();
            List<PmsBrand> list2 = future2.get();
            map.put("list",list);
            map.put("list1",list1);
            map.put("list2",list2);
        }catch (InterruptedException | ExecutionException e){
            e.printStackTrace();
        }finally {
//            executor.shutdown();
        }
        return map;
    }
}
